<template>
  <div>
<el-table :data="tabdata" style="width: 100%" v-if="tabdata">
    <el-table-column label="文章ID" width="100">
      <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.id }}</span>
      </template>
    </el-table-column>
    <el-table-column label="文章标题" width="260">
      <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.title }}</span>
      </template>
    </el-table-column>
    <el-table-column label="创建用户" width="180">
      <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.author }}</span>
      </template>
    </el-table-column>
    <el-table-column label="创建时间" width="180">
      <template slot-scope="scope">
        <i class="el-icon-time"></i>
        <span style="margin-left: 10px">{{ scope.row.createtime }}</span>
      </template>
    </el-table-column>
    <el-table-column label="状态" width="180">
      <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.statue==1?'审核通过':scope.row.statue==0?'未通过':'待审核' }}</span>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button size="mini" @click="handlePass(scope.$index, scope.row)"
          v-if="scope.row.statue==2">通过</el-button
        >
        <el-button size="mini" @click="handleNoPass(scope.$index, scope.row)"
          v-if="scope.row.statue==2">不通过</el-button
        >
         <el-button size="mini" @click="handleCommend(scope.$index, scope.row)"
          v-if="scope.row.recommend==0&&scope.row.statue==1">推荐</el-button
        >
        <el-button size="mini" @click="handleCancelCommend(scope.$index, scope.row)"
          v-if="scope.row.recommend==1&&scope.row.statue==1">取消推荐</el-button
        >
        <el-button
          size="mini"
          type="danger"
          @click="handleDeleteBlog(scope.$index, scope.row)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <el-table :data="tableData" style="width: 100%" v-if="tableData" align="center">
    <el-table-column label="用户ID" width="100">
      <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.id }}</span>
      </template>
    </el-table-column>
    <el-table-column label="用户邮箱" width="200">
      <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.email }}</span>
      </template>
    </el-table-column>
    <el-table-column label="用户昵称" width="150">
      <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.nickname }}</span>
      </template>
    </el-table-column>
    <el-table-column label="用户角色" width="150">
      <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.role==1?'管理员':'普通用户' }}</span>
      </template>
    </el-table-column>
    <el-table-column label="创建时间" width="150">
      <template slot-scope="scope">
        <i class="el-icon-time"></i>
        <span style="margin-left: 10px">{{ scope.row.createtime }}</span>
      </template>
    </el-table-column>
    <el-table-column label="用户状态" width="150">
      <template slot-scope="scope">
        <span style="margin-left: 10px">{{ scope.row.status==1?'正常':'禁用' }}</span>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button size="mini" @click="handleAddBlack(scope.$index, scope.row)"
          >{{scope.row.status==1?'加入黑名':'移出黑名单'}}</el-button
        >
        <el-button size="mini" @click="handleSetAdmin(scope.$index, scope.row)"
          >{{scope.row.role==1?'设为普通用户':'设为管理员'}}</el-button
        >
        <el-button
          size="mini"
          type="danger"
          @click="handleDeleteUser(scope.$index, scope.row)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  </div>
</template>

<script>
export default {
    props:["tabdata","tableData","tabactive"],
  data() {
    return {
      
    };
  },
  methods: {
    //通过审核(0不通过，1通过，2审核中)
     handlePass(index, row){
       let postData = {
         id:row.id,
         status:1
       }
       this.API.checkBlog(postData).then((res) => {
        if (res.message.code == 200) {
          this.$emit('handleBlog');//select事件触发后，自动触发showCityName事件
          this.$message({
            message: res.message.msg,
            type: "success",
          });
        } else {
          this.$message({
            message: res.message.msg,
            type: "error",
          });
        }
      });
      console.log(index, row);
    },
    //不通过审核
     handleNoPass(index, row){
       let postData = {
         id:row.id,
         status:0
       }
       this.API.checkBlog(postData).then((res) => {
        if (res.message.code == 200) {
          this.$emit('handleBlog');//select事件触发后，自动触发showCityName事件
          this.$message({
            message: res.message.msg,
            type: "success",
          });
        } else {
          this.$message({
            message: res.message.msg,
            type: "error",
          });
        }
      });
      console.log(index, row);
    },
    //禁用
     handleBan(index, row){
       let postData = {
         id:row.id,
         status:2
       }
       this.API.checkBlog(postData).then((res) => {
        if (res.message.code == 200) {
          this.$emit('handleBlog');//select事件触发后，自动触发showCityName事件
          this.$message({
            message: res.message.msg,
            type: "success",
          });
        } else {
          this.$message({
            message: res.message.msg,
            type: "error",
          });
        }
      });
      console.log(index, row);
    },
    //推荐
     handleCommend(index, row){
       let postData = {
         id:row.id,
         recomm:1
       }
       this.API.recommedBlog(postData).then((res) => {
        if (res.message.code == 200) {
          this.$emit('handleBlog');//select事件触发后，自动触发showCityName事件
          this.$message({
            message: res.message.msg,
            type: "success",
          });
        } else {
          this.$message({
            message: res.message.msg,
            type: "error",
          });
        }
      });
      console.log(index, row);
    },
    handleCancelCommend(index, row){
      let postData = {
         id:row.id,
         recomm:0
       }
      this.API.recommedBlog(postData).then((res) => {
        if (res.message.code == 200) {
          this.$emit('handleBlog');//select事件触发后，自动触发showCityName事件
          this.$message({
            message: res.message.msg,
            type: "success",
          });
        } else {
          this.$message({
            message: res.message.msg,
            type: "error",
          });
        }
      });
    },
    //添加黑名单
    handleAddBlack(index, row){
      let postData = {
         id:row.id,
         status:0
       }
      if(row.status==1){
        postData.status = 0
      }else{
        postData.status = 1
      }
       this.API.addBackUser(postData).then((res) => {
        if (res.message.code == 200) {
          this.$emit('handleUser');//select事件触发后，自动触发showCityName事件
          this.$message({
            message: res.message.msg,
            type: "success",
          });
        } else {
          this.$message({
            message: res.message.msg,
            type: "error",
          });
        }
      });
    },
    //设为管理员
    handleSetAdmin(index, row) {
      let postData = {
         id:row.id,
         role:0
       }
      if(row.role==1){
        postData.role = 0
      }else{
        postData.role = 1
      }
      this.API.setAdmin(postData).then((res) => {
        if (res.message.code == 200) {
          this.$emit('handleUser');//select事件触发后，自动触发showCityName事件
          this.$message({
            message: res.message.msg,
            type: "success",
          });
        } else {
          this.$message({
            message: res.message.msg,
            type: "error",
          });
        }
      });
      console.log(index, row);
    },
    //删除博客
    handleDeleteBlog(index, row) {
      this.API.deleteBlog(row.id).then((res) => {
        if (res.message.code == 200) {
          this.$emit('handleBlog');//select事件触发后，自动触发showCityName事件
          this.$message({
            message: res.message.msg,
            type: "success",
          });
        } else {
          this.$message({
            message: res.message.msg,
            type: "error",
          });
        }
      });
      console.log(index, row);
    },
    //删除用户
    handleDeleteUser(index, row) {
      this.API.deleteUser(row.id).then((res) => {
        if (res.message.code == 200) {
          this.$emit('handleUser');//select事件触发后，自动触发showCityName事件
          this.$message({
            message: res.message.msg,
            type: "success",
          });
        } else {
          this.$message({
            message: res.message.msg,
            type: "error",
          });
        }
      });
      console.log(index, row);
    },
  },
};
</script>